package cn.ywyu.classfinal.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * 密码输入界面，模拟命令行输入
 */
public class PasswordInputForm implements AutoCloseable {
    //输入提示
    private static final String TIPS = " 项目正在启动，请输入启动密码\r\n Password: ";
    //密码显示字符
    private static final char PASS_CHAR = '*';
    //窗口
    private JDialog frame;
    //文本域
    private JTextArea text;
    //已输入字符长度
    private int pwdLen = 0;
    //已输入的字符，最长100
    private char[] pwdChars = new char[100];
    //是否有下一行
    volatile boolean endInput = false;

    /**
     * 获取输入的密码
     *
     * @return 密码char数组
     */
    public char[] getPassword() {
        while (!endInput) {
            try {
                Thread.sleep(100);
            } catch (Exception ignored) {
            }
        }

        char[] chars = new char[pwdLen];
        System.arraycopy(pwdChars, 0, chars, 0, pwdLen);

        pwdLen = 0;
        pwdChars = new char[100];
        return chars;
    }

    /**
     * 显示窗口
     *
     * @return 显示是否成功
     */
    public boolean showForm() {
        try {
            frame = new JDialog();
            frame.setTitle("项目启动密码 - ClassFinal");
            frame.setSize(560, 320);
            frame.setResizable(false);
            frame.setLocationRelativeTo(null);
            frame.setAlwaysOnTop(true);
            frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            text = new JTextArea();
            text.setFont(new Font(null, Font.PLAIN, 18));
            text.setBackground(new Color(0, 0, 0));
            text.setForeground(new Color(0, 255, 0));
            text.setText(TIPS);
            text.addKeyListener(getKeyAdapter());
            text.enableInputMethods(false);
            frame.add(text);
            frame.setVisible(true);

            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 关闭窗口
     */
    @Override
    public void close() {
        frame.setVisible(false);
        frame.dispose();
    }

    /**
     * 键盘监听事件
     *
     * @return KeyAdapter
     */
    private KeyAdapter getKeyAdapter() {
        return new PasswordKeyAdapter();
    }

    private class PasswordKeyAdapter extends KeyAdapter {
        /**
         * 按下
         */
        @Override
        public void keyPressed(KeyEvent e) {
            char keyChar = e.getKeyChar();
            if (keyChar == KeyEvent.VK_BACK_SPACE) {
                if (pwdLen > 0) {
                    pwdChars[pwdLen--] = 0;
                }
            } else if (keyChar == KeyEvent.VK_ENTER || keyChar == KeyEvent.VK_ESCAPE) {
                endInput = true;
                close();
            }
        }

        /**
         * 键入
         */
        @Override
        public void keyTyped(KeyEvent e) {
            char keyChar = e.getKeyChar();
            if (keyChar > KeyEvent.VK_SPACE && keyChar < KeyEvent.VK_DELETE) {
                pwdChars[pwdLen++] = keyChar;
            } else if (keyChar == KeyEvent.VK_BACK_SPACE) {
                if (pwdLen <= 0) {
                    text.setText(TIPS);
                }
            }
        }

        /**
         * 释放
         */
        @Override
        public void keyReleased(KeyEvent e) {
            StringBuilder displayText = new StringBuilder(TIPS);
            for (int i = 0; i < pwdLen; i++) {
                displayText.append(PASS_CHAR);
            }
            text.setText(displayText.toString());
        }
    }
}
